package com.example.orderfoodsystem22201310liuziming.controller;

import com.example.orderfoodsystem22201310liuziming.entity.foodmenu;
import com.example.orderfoodsystem22201310liuziming.repository.MenuRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Optional;

@Controller
@RequestMapping("menu")
public class MenuController {
    @Autowired //接线
    private MenuRepository menuRepository;

    @RequestMapping("/haltsales/{Id}/{Status}")   //菜品停售
    protected String haltSales(@PathVariable("Id") Integer id, @PathVariable("Status") String sts) {
        Optional<foodmenu> foodmenuOptional = menuRepository.findById(id);  //查找
        if (foodmenuOptional.isEmpty()) {
            System.out.println("不存在记录");
        }
        foodmenu closesale = foodmenuOptional.get();
        closesale.setFoodstatus(-1);
        menuRepository.save(closesale);
        return "seller/SellerMenuList";
    }

    @RequestMapping("/opensales/{Id}/{Status}")  //菜品开售
    protected String openSales(@PathVariable("Id") Integer id, @PathVariable("Status") String sts) {
        Optional<foodmenu> foodmenuOptional = menuRepository.findById(id);
        if (foodmenuOptional.isEmpty()) {
            System.out.println("不存在记录");
        }
        foodmenu opensale = foodmenuOptional.get();
        opensale.setFoodstatus(1);
        menuRepository.save(opensale);
        return "seller/SellerMenuList";
    }

    @RequestMapping("/deletesales/{Id}")  //删除菜品
    protected String deleteSales(@PathVariable("Id") Integer id) {
        Optional<foodmenu> foodmenuOptional = menuRepository.findById(id);
        if (foodmenuOptional.isEmpty()) {
            System.out.println("不存在记录");
        }
        menuRepository.deleteById(id);
        return "seller/SellerMenuList";
    }

    @RequestMapping("/edit/{id}") //更改菜品前查找
    protected String editMenu(Model model, @PathVariable("id") Integer id) {
        Optional<foodmenu> menuOptional = menuRepository.findById(id);
        if (menuOptional.isEmpty()) {
            model.addAttribute("msg", "没有发现记录");
            return "/seller/SellerMessage";
        } else {
            model.addAttribute("foodmenu", menuOptional.get());
            return "seller/SellerMenuEdit";
        }
    }

    @RequestMapping("/editUpdate")      //更新菜品页
    protected String editUpdate(Model model,
                                @RequestParam("id") Integer id,
                                @RequestParam("foodname") String foodname,
                                @RequestParam("foodprice") float foodprice
    ) {
        try {
            foodmenu foodmenu = new foodmenu();
            foodmenu.setId(id);
            foodmenu.setFoodname(foodname);
            foodmenu.setFoodprice(foodprice);
            menuRepository.save(foodmenu);
            model.addAttribute("msg","菜品更新成功");
            return "/seller/SellerMessage";
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("/new") //添加菜品
    protected String newfood() {
        return "seller/SellerNewFood";
    }

    @RequestMapping("/newSave")    //添加菜品页
    protected String newSave(Model model,
                             @RequestParam("foodname") String name,
                             @RequestParam("foodprice") float price
    ) {
        try {
            foodmenu foodmenu = new foodmenu();
            foodmenu.setFoodname(name);
            foodmenu.setFoodprice(price);
            foodmenu.setFoodstatus(1);
            menuRepository.save(foodmenu);
            model.addAttribute("msg", "菜品添加成功");
            return "/seller/SellerMessage";
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

//    @RequestMapping("/menudelete/{Id}")//!
//    @ResponseBody
//    protected String menuDelete(@PathVariable("Id") Integer id, @PathVariable("status")String sts){ //传参
//        @RequestMapping("/menudelete")   //实际地址/menudelete?烤鸭=5
//        @ResponseBody
//        protected String menuDelete(@RequestParam("烤鸭")Integer id){ //传参


        //        foodmenu menu = new foodmenu();
//        menu.setFoodname("烤鸭");
//        menu.setFoodprice(11.2f);
//        menu.setFoodstatus(1);

//        menuRepository.deleteById(id);
//        Optional<foodmenu> foodmenuOptional = menuRepository.findById(id);
//        foodmenu menu = foodmenuOptional.get();
//        menu.setFoodprice(99);
//        menuRepository.save(menu);
//        List<foodmenu> menuList = new ArrayList<>(10);
//        menuList.add(menu);
//        menuRepository.saveAll(menuList);

//        return "deleted ";
//    }
    //    @RequestMapping("/addorder/{Id}/{Order}")  //增加订单
//    @ResponseBody
//    protected String addOrder(@PathVariable("Id") Integer id,@PathVariable("Order")Float order){
//        Optional<foodmenu> foodmenuOptional = menuRepository.findById(id);
//        if(foodmenuOptional.isEmpty()){
//            System.out.println("不存在记录");
//        }
//        foodmenu addorder = foodmenuOptional.get();
//        int afteradd = addorder.getFoodordernumber();
//        afteradd += 1;
//        addorder.setFoodordernumber(afteradd);
//        menuRepository.save(addorder);
//        return "seller/SellerOrder";
//    }
//
//    @RequestMapping("/reduce/{Id}/{Order}")  //完成订单
//    protected String reduceOrder(@PathVariable("Id") Integer id,@PathVariable("Order")Float order){
//        Optional<foodmenu> foodmenuOptional = menuRepository.findById(id);
//        if(foodmenuOptional.isEmpty()){
//            System.out.println("不存在记录");
//        }
//        foodmenu reduceorder = foodmenuOptional.get();
//        int afterreduce = reduceorder.getFoodordernumber();
//        if(afterreduce>0){
//        afterreduce -= 1;
//        reduceorder.setFoodordernumber(afterreduce);
//        menuRepository.save(reduceorder);
//        }
//        return "seller/SellerOrder";
//    }

